# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=zlib-ng
pkgver=2.1.6
pkgrel=1
pkgdesc="zlib replacement with optimizations for next generation systems"
url="https://github.com/zlib-ng/zlib-ng"
arch="all"
license="Zlib"
makedepends="cmake samurai"
checkdepends="gzip xxd zlib-dev"
subpackages="$pkgname-dev"
source="https://github.com/zlib-ng/zlib-ng/archive/$pkgver/zlib-ng-$pkgver.tar.gz
	fix-rvv-detection-riscv64.patch
	"

# secfixes:
#   2.0.6-r0:
#     - CVE-2022-37434

build() {
	local crossopts=
	if [ "$CBUILD" != "$CHOST" ]; then
		crossopts="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	# WITH_UNALIGNED - unaligned access invokes undefined behaviour,
	#   see https://github.com/gentoo/gentoo/pull/17167 for more info.
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		-DWITH_DFLTCC_DEFLATE=ON \
		-DWITH_DFLTCC_INFLATE=ON \
		-DWITH_UNALIGNED=OFF \
		-DZLIB_ENABLE_TESTS="$(want_check && echo ON || echo OFF)" \
		-DZLIBNG_ENABLE_TESTS="$(want_check && echo ON || echo OFF)" \
		-DWITH_GTEST=OFF \
		$crossopts
	cmake --build build
}

check() {
	ctest --test-dir build --output-on-failure
}

package() {
	DESTDIR="$pkgdir" cmake --install build
}

sha512sums="
59ef586c09b9a63788475abfd6dd59ed602316b38f543f801bea802ff8bec8b55a89bee90375b8bbffa3bdebc7d92a00903f4b7c94cdc1a53a36e2e1fd71d13a  zlib-ng-2.1.6.tar.gz
22433ea48e49688acc6ba318a629362e71a704fe148aa8d0cde00ab9c88603ad0923ea4b3d756fa5261fb56801535111ec277b8665f8ac51a2220a8351c09e73  fix-rvv-detection-riscv64.patch
"
